﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Note_Smart_Last
{
    public partial class MainForm : Form
    {
        private Panel contentPanel;
        public MainForm()
        {
            InitializeComponent();
            SetupUI();
            ShowLoginForm(); // 默认显示登录页面
        }

        private void SetupUI()
        {
            this.Text = "笔记系统";
            this.Size = new Size(1000, 700);
            this.StartPosition = FormStartPosition.CenterScreen;

            contentPanel = new Panel
            {
                Dock = DockStyle.Fill
            };
            this.Controls.Add(contentPanel);
        }

        private void ShowLoginForm()
        {
            var loginForm = new LoginForm();
            loginForm.LoginSuccess += username =>
            {
                ShowNoteBookListForm(username);
            };
            ShowFormInPanel(loginForm);
        }

        private void ShowNoteBookListForm(string username)
        {
            var noteBookListForm = new NoteBookListForm(username);
            noteBookListForm.OpenNoteList += notebook =>
            {
                ShowNotesListForm(notebook);
            };
            noteBookListForm.LogoutRequested += () =>
            {
                ShowLoginForm(); // 切换到登录页面
            };
            ShowFormInPanel(noteBookListForm);
        }
        private void ShowNotesListForm(NoteBook notebook)
        {
            var notesListForm = new NotesListForm(notebook.DirectoryPath);

            notesListForm.OpenNoteEditor += note =>
            {
                ShowNoteEditorForm(note, notebook);
            };
            notesListForm.BackToNoteBookList += () =>
            {
                ShowNoteBookListForm(notebook.Username); // 返回到 NoteBookListForm
            };
            notesListForm.LogoutRequested += () =>
            {
                ShowLoginForm(); // 切换到登录页面
            };
            ShowFormInPanel(notesListForm);
        }

        private void ShowNoteEditorForm(Note note, NoteBook notebook)
        {
            var noteEditorForm = new NoteEditorForm(note);
            noteEditorForm.EditorClosed += () =>
            {
                ShowNotesListForm(notebook); // 返回到 NotesListForm
            };
            ShowFormInPanel(noteEditorForm);
        }
        private void ShowFormInPanel(Form form)
        {
            contentPanel.Controls.Clear();
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;
            form.Dock = DockStyle.Fill;
            form.Visible = true;
            contentPanel.Controls.Add(form);
        }
    }
}
